using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace FrameWork.Inventory
{
    /// <summary>
    /// 背包初始化
    /// 物品操作和移除
    /// </summary>
    public class InventoryController : MonoBehaviour
    {
        [Header("库存SO文件")]
        [SerializeField] private InventorySO myInventory;
        
        public int MaxSize { get; private set; } = 20;
        
        private void Awake()
        {
            // 设置背包，背包初始化
            myInventory.InventoryInitialize(MaxSize);

            // 注册事件
            EventSystem.Instance.AddListener<ItemSO, int>(EventName.EVENT_INVENTORY_MODEL_ADDITEM, AddItem);
            EventSystem.Instance.AddListener<ItemSO, int>(EventName.EVENT_INVENTORY_MODEL_REMOVEITEM, RemoveItem);
        }

        private void OnDisable()
        {
            EventSystem.Instance.RemoveListener<ItemSO, int>(EventName.EVENT_INVENTORY_MODEL_ADDITEM, AddItem);
            EventSystem.Instance.RemoveListener<ItemSO, int>(EventName.EVENT_INVENTORY_MODEL_REMOVEITEM, RemoveItem);
        }


        // 添加物品
        private void AddItem(ItemSO item, int quantity)
        {
            myInventory.AddItem(item, quantity);
        }

        // 移除物品
        private void RemoveItem(ItemSO item, int quantity)
        {
            myInventory.RemoveItem(item, quantity);
        }
    }
}
